<template>
  <div>
    <Portal to="right-disable"
            :disabled="!queryModelDefaultSetting.showSetting">
      <el-form :inline="true">
        <el-form-item v-loading='loading'>
          <el-checkbox v-model="checkboxvalue">{{ gadgetSetting.label }}</el-checkbox>
        </el-form-item>
      </el-form>
    </Portal>
    <model :showSetting="queryModelDefaultSetting.showSetting" @queryModelClosed="queryModelClosed"
           :dataInfo="queryModelDefaultSetting.dataInfo"
           :updateGadget="upgradeGadgetSetting"/>
  </div>
</template>

<script>
import mixinGadgetViewModule from '@/mixins/gadget-view-module'
// import model from '@/components/gadget/query-model/query-model.vue'
import { defaultGadgetSettingList } from '@/utils/data-json'
export default {
  mixins: [mixinGadgetViewModule],
  components: { model: () => import('@/components/gadget/query-model/query-model') },
  data () {
    return {
      mixinViewModuleOptions: {
        pageId: this.pageId
      },
      gadgetStatus: 'init',
      checkboxvalue: false,
      loading: false,
      gadgetSetting: {},
      queryModelDefaultSetting: {
        showSetting: false,
        dataInfo: {
          businessId: this.pageId,
          gadgetId: this.id,
          gadgetType: 'checkboxgadget',
          gadgetSettingExclude: ['DATA'],
          gadgetSetting: this._.find(defaultGadgetSettingList, { gadgetType: 'checkboxgadget' }).gadgetSetting
          // gadgetSetting: {
          //   label: '',
          //   queryParams: 'checkboxValue',
          //   trueValue: true,
          //   falseValue: false,
          //   autoRunReport: false,
          //   defaultValue: null
          // }
        }
      }

    }
  },
  beforeCreate () {
    // this.$options.components.model = () => import('@/components/gadget/query-model/query-model')
  },
  created () {
    this.query()
    this.$store.state.isWidget = false
  },
  watch: {
    checkboxvalue (val) {
      this.getDataListParams()
      if (this.gadgetSetting.autoRunReport) {
        this.publishMessage('runReport')
      }
    }
  },
  methods: {
    resetslicer () {
      this.getDataListParams()
    },
    getDataListParams () {
      var _self = this
      var newslicer = {}
      if (this.checkboxvalue) {
        newslicer[_self.gadgetSetting.queryParams] = this.gadgetSetting.trueValue
      } else {
        newslicer[_self.gadgetSetting.queryParams] = this.gadgetSetting.falseValue
      }
      _self._.merge(_self.slicer, newslicer)
    },
    queryModelClosed () {
      this.queryModelDefaultSetting.showSetting = false
      this.query()
    },
    showQueryModel () {
      this.queryModelDefaultSetting.showSetting = true
    },
    query () {
      this.loading = true
      this._.merge(this.gadgetSetting, this.queryModelDefaultSetting.dataInfo.gadgetSetting)
      var _self = this
      const params = {
        // businessId: this.pageId,
        gadgetId: this.id,
        includeSql: false,
        code: this.code,
        gadgettype: this.wgadgettype,
        widgetName: this.wName
      }
      this.$interface.reports.getReportSettingInfo(params).then(({ data: res }) => {
        if (res.code !== 0) {
          return _self.$message.error(res.msg)
        }
        if (res.data) {
          _self._.merge(_self.gadgetSetting, res.data.gadgetSetting)
        }
        if (_self.gadgetSetting.defaultValue !== null) {
          _self.checkboxvalue = _self.gadgetSetting.defaultValue
          _self.getDataListParams()
        } else {
          _self.resetslicer()
        }
        _self.updateGadgetStatus('ready') //! IMPORTANT must have this code in every gadget
        _self.loading = false
      })
    }
  }
}
</script>
<style scoped>

</style>
